package com.atwork.wuhua.mvp.presenter;

import com.atwork.wuhua.base.BasePresenter;
import com.atwork.wuhua.base.BaseReq;
import com.atwork.wuhua.bean.UserBean;
import com.atwork.wuhua.dialog.CenterDialog;
import com.atwork.wuhua.mvp.Callback;
import com.atwork.wuhua.mvp.DataModel;
import com.atwork.wuhua.mvp.Token;
import com.atwork.wuhua.mvp.view.ISetUpActivity;
import com.atwork.wuhua.utils.SPUtils;
import com.atwork.wuhua.utils.UserUtil;

public class SetUpPresenter extends BasePresenter<ISetUpActivity> {

    public void setLogout() {
        CenterDialog centerDialog = new CenterDialog(getView().getContext(), "确定退出程序", new CenterDialog.OnDialogClick() {
            @Override
            public void dialogClick(int switchButton) {
                postLogout();

            }
        });
        centerDialog.show();
    }

    public void postLogout() {
        showLoading();
        UserBean userModel = SPUtils.getObject(getView().getContext(), SPUtils.USER_OBJECT, UserBean.class);
        BaseReq req = new BaseReq();
        req.setKey("uuid", userModel.getData().getUuid());
        if (isViewAttached()) {
            DataModel.request(Token.API_LOGOUT)
                    .params(req)
                    .execute(new Callback<String>() {

                        @Override
                        public void onSuccess(String msg, String data) {
                            UserUtil.loginOut(getView().getContext());
                            showToast(msg);
                            getView().onLogout();
                            hideLoading();
                        }

                        @Override
                        public void onFailure(String msg) {
                            showToast(msg);
                        }

                        @Override
                        public void onError() {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }
}
